/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package percussiongenerator.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.sound.midi.Instrument;
import javax.swing.BoxLayout;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.basics.Observer;
import percussiongenerator.gui.view.MetronomeView;
import percussiongenerator.gui.view.TimeLineView;
import percussiongenerator.gui.view.View;
import percussiongenerator.languages.ILanguageDependent;
import percussiongenerator.sound.IMidiPlayer;
import percussiongenerator.model.ITrackCollectionListener;
import percussiongenerator.model.Track;
import percussiongenerator.model.TrackCollection;

/**
 *
 * @author Jannes Plyson
 */
public class TrackCollectionPanel extends JPanel implements ITrackCollectionListener, ILanguageDependent, Observer{
    private JPanel collectionPanel;
    private TrackCollection trackCollection;
    private JScrollPane trackCollectionScrollPane;
    private JScrollPane trackOptionsScrollPane;
    private TrackOptionsPanel trackOptionsPanel;
    private MutableValueNode<View> selectedView;
    
    public TrackCollectionPanel(IMidiPlayer midiPlayer) {                
        selectedView = new BasicValueNode(null);
        selectedView.addObserver(this);
        trackCollection = new TrackCollection(midiPlayer);
        trackCollection.addListener(this);
        this.setLayout(new BorderLayout());        
        collectionPanel = new JPanel();
        collectionPanel.setBackground(Color.WHITE);
        collectionPanel.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));        
        trackCollectionScrollPane = new JScrollPane(collectionPanel);
        this.add(trackCollectionScrollPane,BorderLayout.CENTER);
        collectionPanel.setLayout(new BoxLayout(collectionPanel, BoxLayout.PAGE_AXIS));
        addMetronome(midiPlayer);           
        trackOptionsScrollPane = new JScrollPane();
        trackOptionsScrollPane.setPreferredSize(new Dimension(250,300));
        InputMap im = trackOptionsScrollPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        im.put(KeyStroke.getKeyStroke("LEFT"), "none");
        im.put(KeyStroke.getKeyStroke("RIGHT"), "none");
        trackOptionsPanel = null;
        this.add(trackOptionsScrollPane,BorderLayout.EAST);        
        
    }    

    public MutableValueNode<View> getSelectedViewNode(){
        return selectedView;
    }

    private void addMetronome(IMidiPlayer midiPlayer){
        Instrument[] instruments = midiPlayer.getInstruments();
        int i = 0;
        while(i< instruments.length && !instruments[i].toString().contains("Drumkit:")){
            i++;
        }
        Instrument instrument;
        if(i < instruments.length){
            instrument = instruments[i];
        }else{
            instrument = instruments[0];
        }
        trackCollection.addTrack(Track.createMetronome(instrument, 120));
        TrackPanel trackPanel = getTrackPanels().get(0);
        trackPanel.removeView(TimeLineView.class);
        trackPanel.addView(MetronomeView.class);
    }

    public void play() {
        trackCollection.play();
    }

    public void pause() {
        trackCollection.pause();
    }
    
    public void stop() {
        trackCollection.stop();
    }

    public Instrument[] getInstruments() {
        return trackCollection.getInstruments();
    }

    public void addToMidiPlayer(){
        trackCollection.addToMidiPlayer();
    }

    public TrackCollection getTrackCollection(){
        return trackCollection;
    }

    private ArrayList<TrackPanel> getTrackPanels(){
        ArrayList<TrackPanel> result = new ArrayList<TrackPanel>();
        Component[] components = collectionPanel.getComponents();
        for(Component c : components){
            if(c instanceof TrackPanel){
                result.add((TrackPanel)c);
            }
        }
        return result;
    }          

    public void updateLanguage(ResourceBundle labels) {
        ArrayList<TrackPanel> trackPanels = getTrackPanels();
        for (int i = 0; i < trackPanels.size(); i++) {
            trackPanels.get(i).updateLanguage(labels);
        }        
    }           

    

    private void updatePanel(){
        this.revalidate();
        this.repaint();
    }

    public void trackAdded(Track track) {        
        TrackPanel tp = new TrackPanel(track,selectedView);        
        collectionPanel.add(tp);        
        updatePanel();
    }

    public void trackRemoved(Track track) {
        Component[] components = collectionPanel.getComponents();
        for(Component c : components){
            if(c instanceof TrackPanel && ((TrackPanel)c).getTrack() == track){
                collectionPanel.remove(c);
            }
        }
        updatePanel();
    }

    public void cleared() {
        removeAll();
        updatePanel();
    }

    public void trackUpdated(Track track) {
        updatePanel();
    }

    public void onNotify(Object source){
        trackSelected();
    }

    public void trackSelected(){
        if(trackOptionsPanel != null){
            trackOptionsPanel.removeRelations();
        }
        trackOptionsPanel = new TrackOptionsPanel(selectedView.getValue().getTrack(),selectedView.getValue(),getInstruments());
        trackOptionsScrollPane.getViewport().removeAll();
        trackOptionsScrollPane.getViewport().add(trackOptionsPanel);
        updatePanel();
    }
}
